﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDataClassLib.AzureDSTableAdapters;
using MyDataClassLib;
using System.Text;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.Data.SqlClient;
using System.Configuration;

namespace WebRole1
{
    public partial class _Default : System.Web.UI.Page
    {
        private static readonly string SQLAzureConnectionString = RoleEnvironment.GetConfigurationSettingValue("SQLAzureConnectionString");
        private static readonly string SQLServerConnectionString = RoleEnvironment.GetConfigurationSettingValue("SQLServerConnectionString");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                lblResult.Text = "Please enter a customer value and select a parameter from the check box.";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                GetCustomerByValue();

            }
            catch (Exception ex)
            {
                lblResult.Text = ex.Message;
            }
        }

        public void GetCustomerByValue()
        {
            GridView1.DataSourceID = "";
            GridView1.DataSource = null;
            GridView1.DataBind();

            bool useSQLServer = false;
            if (ddlDatabase.Text != "SQLAzure")
            {
                useSQLServer = true;
            }

            string connectionString = (useSQLServer) ? SQLServerConnectionString : SQLAzureConnectionString;
            DateTime before = DateTime.Now;
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                CustomerDataTableAdapter ca = new CustomerDataTableAdapter();
                ca.Connection = con;
                string parameter = txtCustomerInformaton.Text;
                if (cbEmail.Checked)
                {

                    GridView1.DataSource = ca.GetDataByEmail(parameter);
                    GridView1.DataBind();


                }
                else if (cbName.Checked)
                {

                    GridView1.DataSource = ca.GetDataByName(parameter);
                    GridView1.DataBind();

                }
                else if (cbPhone.Checked)
                {

                    GridView1.DataSource = ca.GetDataByPhone(parameter); ;
                    GridView1.DataBind();
                }
                else if (cbState.Checked)
                {

                    GridView1.DataSource = ca.GetDataByState(parameter);
                    GridView1.DataBind();
                }
                else
                {

                    lblResult.Text = "Please select a customer parameter.";
                    return;
                }

                double responseTimeInMs = (DateTime.Now - before).TotalMilliseconds;
                lblResponseTime.Text = "Response Time in milliseconds " + responseTimeInMs;
            }//using

        }

        private void DisplayCustomer(AzureDS.CustomerDataDataTable custDataTable)
        {
           
            GridView1.DataSource = custDataTable;
            GridView1.DataBind();
            
         

        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                GridView1.DataSource = null;
                GridView1.DataBind();
                lblResponseTime.Text = "";
                
            }
            catch (Exception ex)
            {
                lblResult.Text = ex.Message;
            }
        }

        protected void btnWebCfg_Click(object sender, EventArgs e)
        {
            try
            {

                string value1 = ConfigurationManager.AppSettings["name1"];

                string value2 = ConfigurationManager.AppSettings["name2"];

                lblAppSettings.Text = string.Format("Name1={0}, Name2={1}", value1, value2);

            }
            catch (Exception ex)
            {
                lblAppSettings.Text = ex.Message;

            }
        }

      
    }
}
